//
//  SingleAnalyzeBarrageView.swift
//  MM2
//
//  Created by 李成明 on 2022/1/5.
//

import UIKit

enum EmotionType {
    case anger(value: Float = 0)
    case disgust(value: Float = 0)
    case fear(value: Float = 0)
    case happiness(value: Float = 0)
    case neutral(value: Float = 0)
    case sadness(value: Float = 0)
    case surprise(value: Float = 0)
    
    var desInt: Int {
        switch self {
        case .anger:
            return 1
        case .disgust:
            return 2
        case .fear:
            return 3
        case .happiness:
            return 4
        case .neutral:
            return 5
        case .sadness:
            return 6
        case .surprise:
            return 7
        }
    }
}

enum FaceAnalyzeResultType {
    case faceValue(value: Float)        // 颜值
    case gender(value: Int)             // 性别   1 男的 2女的
    case age(value: Int)                // 年龄
    case smile(value: Float)            // 微笑
    case emotion(value: Int)            // 情绪 1愤怒 2厌恶 3恐惧 4高兴 5平静 6伤心 7惊讶
    case skinHealth(value: Float)       // 皮肤健康
    case skinStain(value: Float)        // 色斑
    case skinAcne(value: Float)         // 青春痘
    case skinDarkCircle(value: Float)   // 黑眼圈
    
    var img: UIImage {
        switch self {
        case .faceValue:
           return UIImage(named: "face_result_value")!
        case .gender(let value):
            if value == 1 {
                return UIImage(named: "face_result_men")!
            } else {
                return UIImage(named: "face_result_women")!
            }
        case .age:
            return UIImage(named: "face_result_age")!
        case .smile:
            return UIImage(named: "face_result_smile")!
        case .emotion:
            return UIImage(named: "face_result_emotion")!
        case .skinHealth:
            return UIImage(named: "face_result_skin_health")!
        case .skinStain:
            return UIImage(named: "face_result_skin_stain")!
        case .skinAcne:
            return UIImage(named: "face_result_skin_acne")!
        case .skinDarkCircle:
            return UIImage(named: "face_result_dark_circle")!
        }
    }
    
    var title: String {
        switch self {
        case .faceValue:
            return "颜值"
        case .gender:
            return "性别"
        case .age:
            return "年龄"
        case .smile:
            return "微笑"
        case .emotion:
            return "情绪"
        case .skinHealth:
            return "皮肤"
        case .skinStain:
            return "色斑"
        case .skinAcne:
            return "青春痘"
        case .skinDarkCircle:
            return "黑眼圈"
        }
    }
    
    var valueStr: String {
        switch self {
        case .faceValue(let value):
            return String(format: "%.2f", value)
        case .gender(let value):
            if value == 1 {
                return "男"
            } else {
                return "女"
            }
        case .age(let value):
            return "\(value)"
        case .smile(let value):
            return String(format: "%.2f", value)
        case .emotion(let value):
            // 1愤怒 2厌恶 3恐惧 4高兴 5平静 6伤心 7惊讶
            switch value {
            case 1:
                return "愤怒"
            case 2:
                return "厌恶"
            case 3:
                return "恐惧"
            case 4:
                return "高兴"
            case 5:
                return "平静"
            case 6:
                return "伤心"
            case 7:
                return "惊讶"
            default :
                return "平静"
            }
        case .skinHealth(let value):
            return String(format: "%.2f", value)
        case .skinStain(let value):
            return String(format: "%.2f", value)
        case .skinAcne(let value):
            return String(format: "%.2f", value)
        case .skinDarkCircle(let value):
            return String(format: "%.2f", value)
        }
    }
}

class SingleAnalyzeBarrageView: UIView {
    
    init(frame: CGRect, isLeft: Bool) {
        super.init(frame: frame)
        self.isLeft = isLeft
        
        addsubViews([bgView, icon, title, valueLabel])
        bgView.snp.makeConstraints { make in
            make.edges.equalTo(self)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var viewWidth: CGFloat = 0
    let viewHeight:CGFloat = 30
    static var staticViewHeight: CGFloat = 30
    
    var faceAnalyzeType: FaceAnalyzeResultType = .age(value: 0) {
        didSet {
            icon.image = faceAnalyzeType.img
            title.text = faceAnalyzeType.title
            valueLabel.text = faceAnalyzeType.valueStr
            
            updateUI()
        }
    }
    
    private var isLeft = true
    
    private lazy var bgView: UIView = {
        let view = UIView()
        view.layer.masksToBounds = true
        view.layer.cornerRadius = viewHeight/2
        view.backgroundColor = .black
        view.alpha = 0.4
        return view
    }()
    
    private lazy var icon: UIImageView = {
        let view = UIImageView()
        view.image = faceAnalyzeType.img
        return view
    }()
    
    private lazy var title: UILabel = {
        let view = UILabel()
        view.text = faceAnalyzeType.title
        view.textColor = .white
        view.font = .systemFont(ofSize: 16)
        return view
    }()
    
    private lazy var valueLabel: UILabel = {
        let view = UILabel()
        view.text = faceAnalyzeType.valueStr
        view.textColor = .initWithRGB(135, 206, 250)
        view.font = UIFont(name: "Verdana-Italic", size: 20)
        return view
    }()
}

extension SingleAnalyzeBarrageView {
    private func updateUI() {
        
        if isLeft {
            viewWidth = 8
            icon.frame = CGRect(x: 8, y: viewHeight/2 - 10, width: 20, height: 20)
            viewWidth += 20
            
            let titleSize = title.sizeThatFits(CGSize(width: 300, height: CGFloat.greatestFiniteMagnitude))
            title.frame = CGRect(x: viewWidth + 8, y: 0, width: titleSize.width, height: viewHeight)
            viewWidth += (8 + titleSize.width)
            
            let valueSize = valueLabel.sizeThatFits(CGSize(width: 300, height: CGFloat.greatestFiniteMagnitude))
            valueLabel.frame = CGRect(x: viewWidth + 8, y: 0, width: valueSize.width, height: viewHeight)
            
            viewWidth += (8 + valueSize.width + 8)
        } else {
            viewWidth = 8
            let valueSize = valueLabel.sizeThatFits(CGSize(width: 300, height: CGFloat.greatestFiniteMagnitude))
            valueLabel.frame = CGRect(x: viewWidth, y: 0, width: valueSize.width, height: viewHeight)
            viewWidth += (valueSize.width + 8)
            
            let titleSize = title.sizeThatFits(CGSize(width: 300, height: CGFloat.greatestFiniteMagnitude))
            title.frame = CGRect(x: viewWidth, y: 0, width: titleSize.width, height: viewHeight)
            viewWidth += (titleSize.width + 8)
            
            icon.frame = CGRect(x: viewWidth, y: viewHeight/2 - 10, width: 20, height: 20)
            viewWidth += 28
        }
    }
    
    func startAnimation(duration: Double = 0.25,  startFrame: CGRect, endFrame: CGRect, endComplete: ((SingleAnalyzeBarrageView) -> Void)? = nil) {
        self.frame = startFrame
        UIView.animate(withDuration: duration) {
            self.frame = endFrame
        } completion: { _ in
            endComplete?(self)
        }
    }
}
